import $service from '../../../../service.js';
const app = getApp();
let Timer = null;
let t = 59;
Page({
    data: {
        old_user_paw: '',
        new_user_paw: '',
        new_user_paw2: '',
        phone: '',
        msCode: '',
        countTime: 59,
        isSend: false,
        userType: ''
    },
    onShow() {
        console.log(JSON.parse($service.getStore('userInfo')).type)
        this.setData({
            userType: JSON.parse($service.getStore('userInfo')).type
        })
    },
    updatePassword() {
        let params = {
            "confirmPassword": this.data.new_user_paw2,
            "mobile": this.data.phone,
            "newPassword": this.data.new_user_paw,
            "nowPassword": this.data.old_user_paw,
            "smsCode": this.data.msCode
        }
        app.http.updatePassword(params).then(res => {
            this.setData({
                old_user_paw: '',
                new_user_paw: '',
                new_user_paw2: '',
                smsCode: '',
                confirmPassword: '',
                mobile: ''
            })
            $service.showToast(res.msg)
        }).catch(err => {
            $service.showToast(err.msg)
        })
    },
    inputChange(e) {
        var name = e.detail.value;
        var index = e.currentTarget.dataset.index;
        if (index == 1) {
            this.data.phone = name;
        } else if (index == 2) {
            this.data.new_user_paw = name;
        } else if (index == 3) {
            this.data.new_user_paw2 = name;
        } else if (index == 4) {
            this.data.msCode = name;
        } else if (index == 5) {
            this.data.old_user_paw = name;
        }
    },

    // 发送短信
    sendCode() {
        let parmas = {
            "mobile": this.data.phone,
            "type": 1
        }
        app.http.phoneCaptcha(parmas).then(res => {
            $service.showToast(res.msg);
            this.setData({
                isSend: true,
            })
            this.count()
        }).catch(err => {
            $service.showToast(err.msg);
        })
    },
    // 计时器
    count() {
        clearInterval(Timer)
        Timer = setInterval(() => {
            if (t == 1) {
                this.setData({
                    isSend: false
                })
                clearInterval(Timer)
                return
            }
            t--;
            this.setData({
                countTime: t
            })
        }, 1000);
    },
})